package com.example.lib_widget.redpacket;



import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.lib_widget.R;
import androidx.annotation.Nullable;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

/**
 * @author yaotianxue
 * @description
 * @date 2025/2/28
 */
public class RedPacketWidget extends LinearLayout {
    private TextView tvMsg;
    private RedPacketView redPacketView;
    private TranslateAnimation translateAnimation;
    public RedPacketWidget(Context context) {
        this(context, null);
    }

    public RedPacketWidget(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public RedPacketWidget(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr,0);
    }

    public RedPacketWidget(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView(context);
    }
    private void initView(Context context) {
        removeAllViews();
        View view = LayoutInflater.from(context).inflate(R.layout.layout_redpacketwidget,null,false);
        tvMsg = view.findViewById(R.id.tv_redpacket_num);
        redPacketView = view.findViewById(R.id.rpv_redpacket);
        addView(view);
        //上面文字默认不显示
        tvMsg.setVisibility(View.INVISIBLE); //占位置
        //是否保持最终状态？不保持，使用补间动画
        translateAnimation = new TranslateAnimation(0,0,0,-70);
        translateAnimation.setDuration(2000);
        translateAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }
    //订阅
    @Subscribe
    public void onEvent(RedPacketEvent event){
        switch (event.getState()){
            case INIT:
                redPacketView.startAnim();
                redPacketView.setCallback(new RedPacketView.Callback() {
                    @Override
                    public void start() {

                    }

                    @Override
                    public void pause(int angle) {

                    }

                    @Override
                    public void resume() {

                    }

                    @Override
                    public void completed() {
                        //文字弹出动画
                        tvMsg.startAnimation(translateAnimation);
                        //继续
                        redPacketView.startAnim();
                        //回调完成
                        if(stateCallback != null){
                            stateCallback.completed();
                        }

                    }

                    @Override
                    public void cancel() {

                    }
                });
                break;
            case PAUSE:
                redPacketView.pauseAnim();
                break;
            case RESUME:
                redPacketView.resumeAnim();
                break;
            default:
                break;
        }
    }
    //注册EventBus
    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        EventBus.getDefault().register(this);
    }
    //解除注册
    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        EventBus.getDefault().unregister(this);
        //动画停止
        redPacketView.cancelAnim();
        if(translateAnimation != null){
            translateAnimation.cancel();
            translateAnimation = null;
        }
    }

    public void setNum(int num){
        tvMsg.setText("+"+num);
    }

    private StateCallback stateCallback;

    public void setStateCallback(StateCallback stateCallback) {
        this.stateCallback = stateCallback;
    }

    /**
     * 状态回调接口
     */
    public interface StateCallback{
        void completed();
    }
}
